import http from '../vendor/http';
import env from '../factory/env';
import { getPhone } from './global';
import { reactive } from 'vue';
import { state as userState } from './user';
import { Order } from '../vendor/order_food/order';
type RunRecord = {
	td_cash: number;
	td_orders: number;
	can_withdrawal: number;
};


type ImOrder = {
	td_cash: number;
	td_orders: number;
	can_withdrawal: number;
	order: Order
}

export type State = {
	socketTask: UniNamespace.SocketTask;
	fd: string;
};

// 实例化为state
export const state = reactive({ socketTask: null, fd: '' } as State);

export const setSocketTask = (socketTask: UniNamespace.SocketTask) => {
	state.socketTask = socketTask;
};

export const setFd = (fd: string) => {
	state.fd = fd;
};

export const initSocket = (cal: Function = () => {}) => {
	console.log(env.wss)
	var socketTask = uni.connectSocket({
		url: env.wss,
		complete: () => {}
	});
	socketTask.onOpen(() => {
		let lock = false;
		// 将fd与当前商户绑定
		socketTask.onMessage((event:any) => {
			console.log("socket 接收数据:",event)
			const fd = event.data;
			// 初始化第一次接收的数据为fd
			if (!lock) {
				setFd(fd);
				lock = true;
				http.fetchPost('/bindSokcet', { fd, phone: getPhone() }).then(() => {
					cal();
				});
			} else {
				// 绑定socket全局监听
				console.log(event)	
				const d: any = JSON.parse(event.data);
				switch (d.code) {
					case 'runRecord':
						runRecord(JSON.parse(d.data));
						break;
					case 'order':
						order(JSON.parse(d.data));
						break;
				}
			}
		});
	});
	socketTask.onClose(() => {
		setFd('');
		reConnectSocket();
	});
	setSocketTask(socketTask);
};
let t1: any = null;
let t2: any = null;
let t3: any = null;
let t4: any = null;
let t5: any = null;
let t6: any = null;
let t7: any = null;
let t8: any = null;

const clear = () => {
	if (state.fd) {
		clearTimeout(t1);
		clearTimeout(t2);
		clearTimeout(t3);
		clearTimeout(t4);
		clearTimeout(t5);
		clearTimeout(t6);
		clearTimeout(t7);
		clearTimeout(t8);
	} else {
		initSocket();
	}
};

const reConnectSocket = () => {
	// 重连socket
	const seconds = 60000; // 60s

	// 1min
	t1 = setTimeout(() => {
		clear();
	}, 1 * seconds);

	// 30mins
	t2 = setTimeout(() => {
		clear();
	}, 30 * seconds);

	// 1hr
	t3 = setTimeout(() => {
		clear();
	}, 60 * seconds);

	// 2hrs
	t4 = setTimeout(() => {
		clear();
	}, 120 * seconds);

	// 3hrs
	t5 = setTimeout(() => {
		clear();
	}, 180 * seconds);
	// 4hrs
	t6 = setTimeout(() => {
		clear();
	}, 240 * seconds);

	// 5hrs
	t7 = setTimeout(() => {
		clear();
	}, 300 * seconds);

	// 10hrs
	t8 = setTimeout(() => {
		clear();
	}, 600 * seconds);
};

// 商家数据处理
const runRecord = (d: RunRecord) => {
	userState.store_td_cash = d.td_cash;
	userState.store_td_orders = d.td_orders;
	userState.store_can_withdrawal = d.can_withdrawal;
};

// 商家订单推送
const order = (d:ImOrder) => {
	console.log(d)
	
	userState.store_td_cash = d.td_cash;
	userState.store_td_orders = d.td_orders;
	userState.store_can_withdrawal = d.can_withdrawal;
	userState.busOrders.unshift(d.order)  // 前插
	
	console.log(userState)
};
